<?php
/**
 * @description : confirm the choose plan for user
 * @author      : stephen.mo
 * @date        : 2010/09/02
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');

$current_page = OAuthConfig::PAGE_PLAN_CONFIRM;
$login_id     = (isset($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : NULL;
$access_token = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$call_back    = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
$reback_url   = (isset($_REQUEST['reback_url'])) ? $_REQUEST['reback_url'] : NULL;
$lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$pkg_id       = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
$pkg_version  = (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
$pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
$login_id     = (!empty($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : OAuthUtils::getDefaultLoginId($login_id);
$p_lang       = (isset($_REQUEST['p_lang'])) ? OAuthUtils::chkLang($_REQUEST['p_lang']) : NULL;

//setting vars
$params = array();
$params['oauth_token'] = $access_token;
$params['lang']        = $lang;
$params['login_id']    = $login_id;
$params['call_back']   = urlencode($call_back);
$params['reback_url']  = urlencode($reback_url);

//path setting.
$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER);
$base_back_url    = OAuthConfig::OAuthGetBackUrl($_SERVER);
$header_url       = $base_server_path.OAuthConfig::DEFAULT_HEADER_URL;
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;
$oauth_token_str  = (!empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$login_id_str     = (!empty($login_id)) ? '&login_id='.$login_id : NULL;
$message_url      = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;
$ajax_url         = $base_server_path.OAuthConfig::DEFAULT_AJAX_URL;
$call_back_str    = (isset($call_back) && !empty($call_back)) ? '&call_back='.urlencode($call_back) : NULL;
$reback_url_str   = (isset($reback_url) && !empty($reback_url)) ? '&reback_url='.urlencode($reback_url) : NULL;
$lang_str         = '&lang='.$lang;
$cancel_url       = $ajax_url.'?item=logout'.$call_back_str.$reback_url_str.$lang_str.$oauth_token_str;

//verify the oauth token.
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {
    $ret = OAuthUtils::verifyAccessToken($access_token);
    if (!$ret || !$ret['flag']) {
        $other_msg = array('call_back'=>(isset($call_back) ? urlencode($call_back) : urlencode($reset_page_url)) );
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005,1006),$other_msg);
    }
    //try to get login id from oauth token vars.
    if (empty($login_id)) {
        $user = unserialize($ret['data']['data']);
        $login_id = (!empty($user)) ? OAuthUtils::getLoginIdByEmail($user->user) : NULL;
    }
}

//check user & call_back
if (empty($login_id) || !OAuthUtils::chkIsLogin($login_id,$access_token)) {
    $tmp_base_param = '?call_back='.urlencode($call_back).'&reback_url='.urlencode($reback_url).$oauth_token_str.$login_id_str.'&lang='.$lang.
                      '&pkg_id='.$pkg_id.'&pkg_v='.$pkg_version.'&pukey='.$pukey;              
    $tmp_goback_url = $base_server_path.OAuthConfig::DEFAULT_INFORMATION_URL.$tmp_base_param;
    OAuthUtils::redirectTo($tmp_goback_url);
    exit;
}

//get ptoken & pukey & verify it .
$ptoken = OAuthUtils::getPToken();
if (!empty($pukey) && !empty($ptoken)) {
    $pukey = OAuthUtils::reGenPUKey($pukey,$ptoken);
}
else {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
}

//if login id is not empty then need to check the user can or not to buy a plan.
if (!empty($login_id)) {
    //check user can buy plan.
    $rev = OAuthUtils::chkIfUserCanBuyPlan($login_id);
    //if the user have a exists plan and this plan not expiry then show message to ask user ,his plan is still effective.
    if ((isset($rev) && $rev==1)) {
        $tmp_confirm_url = (isset($call_back)) ? $call_back : '#';
        $other_msg = array('login_id'=>$login_id,'call_back'=>urlencode($tmp_confirm_url));
        OAuthUtils::delPToken();
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1031),$other_msg);
        exit;
    }
}

//get data from memcache.
$email_address = NULL;
$page_language = NULL;
$package_infos = NULL;
$need_save_again = 0;
$fdata = array();
$tmpData = OAuthUtils::getFormData($pukey,$ptoken);
if (!$tmpData->retFlag) {
    $retry_url = OAuthConfig::OAuthGetBuyPlanFirst($params);
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1000),$other_msg);
    exit;
}

$tmpData = $tmpData->data;
$fdata['m_login_id']     = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
$fdata['m_call_back']    = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
$fdata['m_lang']         = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
$fdata['m_reback_url']   = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
$fdata['m_model']        = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
$fdata['p_plan_id']      = (isset($tmpData->p_plan_id)) ? $tmpData->p_plan_id : $pkg_id;
$fdata['p_version_id']   = (isset($tmpData->p_version_id)) ? $tmpData->p_version_id : $pkg_version;
$fdata['p_bill_address'] = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
$fdata['p_lang']         = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
$last_page               = (isset($tmpData->current_page)) ? $tmpData->current_page : NULL;
$back_url_top            = (isset($tmpData->m_step_back_III)&&!empty($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III.'&pukey='.$pukey : $base_back_url.'&pukey='.$pukey;
$email_address           = (!empty($fdata['p_bill_address'])) ? $fdata['p_bill_address'] : NULL;
$page_language           = (!empty($fdata['p_lang'])) ? $fdata['p_lang'] : NULL;
$p_lang                  = (empty($p_lang)) ? $tmpData->p_lang : NULL;

//check allow access pages.
$allow_pages = array(
    OAuthConfig::PAGE_PLAN_INFORMATION,
    OAuthConfig::PAGE_PLAN_PAYMENT,
    OAuthConfig::PAGE_PLAN_FREE,
);
if (!in_array($last_page,$allow_pages)) {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
}

//set new data to memcache.
$fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
$fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
$fdata['m_step_back_III'] = (isset($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III : '';
$fdata['m_step_back_IV']  = $base_back_url;
$fdata['current_page']    = $current_page;
$ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
if (!$ret) {
    //not success need to retry it.
    $retry_url = OAuthConfig::OAuthGetBuyPlanFirst($params);
    $other_msg = array('call_back'=>$retry_url);
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1000),$other_msg);
    exit;
}

//get package informations
$pkgInfo = NULL;
if (!empty($pkg_id) && !empty($pkg_version)) {
    $pkgInfo = OAuthUtils::getPkgInfo($pkg_id,$pkg_version,$lang);
}
//if empty pkg info then goto choose plan page.
if (empty($pkgInfo) || !is_object($pkgInfo)) {
    $tmp_goback_url = '?call_back='.urlencode($call_back).'&reback_url='.$reback_url.$oauth_token_str.$login_in_str.'&lang='.$lang.'&pukey='.$pukey; 
    $retry_url = $base_server_path.OAuthConfig::DEFAULT_CHOOSE_PLAN_URL.$tmp_goback_url;
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1013),$other_msg);
    exit;
}

//page info vars.
$user_confirm_page = OPI::$user_confirmation_page;
$payment_buttons   = OPI::$payment_button_info;
$user_info_page    = OPI::$user_information_page;
$language_info     = OPI::$language_info;

$pkg_title         = $pkgInfo->title;
$pkg_unit_txt      = OAuthModelConfig::getRealUnit($pkgInfo->unit,$pkgInfo->duration,$lang,$pkgInfo->free_period);
$pkg_description   = $pkgInfo->description;
$dollar_char       = strtoupper(substr($pkgInfo->currency_code,0,2));
$pkg_price         = $dollar_char.$pkgInfo->price;
$auto_renew_txt    = (isset($pkgInfo->auto_renew)) ? $user_confirm_page[$lang]['auto_resign'] : NULL;
$pkg_type          = (isset($pkgInfo->pkg_type)) ? $pkgInfo->pkg_type : 0;
$tmp_pkg_info      = (!empty($pkg_id) && !empty($pkg_version)) ? '&pkg_id='.$pkg_id.'&pkg_v='.$pkg_version : NULL;

//setting url 
$base_param_url    = '?call_back='.urlencode($call_back).'&reback_url='.urlencode($reback_url).$oauth_token_str.$login_id_str.'&lang='.$lang.$tmp_pkg_info;
$confirm_url       = $base_server_path.OAuthConfig::DEFAULT_PAYMENT_URL.$base_param_url.'&pukey='.$pukey.'&p_lang='.$page_language;
$change_plan_url   = $base_server_path.OAuthConfig::DEFAULT_CHOOSE_PLAN_URL.$base_param_url.'&pukey='.$pukey;
$change_info_url   = $base_server_path.OAuthConfig::DEFAULT_INFORMATION_URL.$base_param_url.'&pukey='.$pukey;
$change_plan_url   = (isset($tmpData->m_step_back_II)&&!empty($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II.'&pukey='.$pukey : $change_plan_url;
$change_info_url   = (isset($tmpData->m_step_back_III)&&!empty($tmpData->m_step_back_III)) ? $tmpData->m_step_back_III.'&pukey='.$pukey : $change_info_url;

//if package is a free plan then need't payment only generate an orderRef.
if ($pkg_type == OAuthModelConfig::PKG_TYPE_SF) {
    $confirm_url = $base_server_path.OAuthConfig::DEFAULT_NOPAY_URL.$base_param_url.'&pukey='.$pukey.'&p_lang='.$page_language;
}


//setting the form datas into array.
$form = array();
$using_real_env           = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page                     = OAuthModelConfig::M_PAGE_OF_PCONFIRM;
$page_language            = (!empty($page_language)) ? $page_language : $p_lang;
$form['lang']             = $lang;
$form['action']           = $confirm_url;
$form['method']           = 'GET';
$form['title']            = $user_confirm_page[$lang]['page_title'];
$form['pconfirm_title']   = $user_confirm_page[$lang]['user_confirm_title'];
$form['pconfirm_note']    = array('selected'=>$user_confirm_page[$lang]['choosed_plan'],
                                  'account_info'=>$user_confirm_page[$lang]['personal_info'],
                                  'pc_email'=>$user_info_page[$lang]['account_email'],
                                  'pc_language'=>$language_info[$lang]['lang'],
                                  'email_value'=>$email_address,
                                  'language_value'=>$language_info[$lang][$page_language],
                              );
//hidden
$form['hidden']['call_back']   = $call_back;
if (!empty($oauth_token_str)) {
    $form['hidden']['oauth_token'] = $access_token;
}
$form['hidden']['login_id']    = $login_id;
$form['hidden']['p_lang']      = $p_lang;
$form['hidden']['lang']        = $lang;
$form['button']['pukey']       = $pukey;
//pkg info
$form['pkg_info']              = array('title'=>$pkg_title,
                                       'unit'=>$pkg_unit_txt,
                                       'renew'=>$auto_renew_txt,
                                       'description'=>$pkg_description,
                                       'price'=>$pkg_price);

//buttons.
$form['button']['cancel']      = array('value'=>$payment_buttons[$lang]['cancel'],
                                       'link'=>$cancel_url);
$form['button']['change_plan'] = array('value'=>$payment_buttons[$lang]['change'],
                                       'link'=>$change_plan_url,);
$form['button']['change_info'] = array('value'=>$payment_buttons[$lang]['change'],
                                       'link'=>$change_info_url,);
$form['button']['previous']    = array('value'=>$payment_buttons[$lang]['previous'],
                                       'link'=>$change_info_url,);
$form['button']['next']        = array('value'=>$payment_buttons[$lang]['next'],
                                       'link'=>$confirm_url,);
$form['a_step']                = 'three';


//include thie template for plan cmodel page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
